package com.hnx.admin.aurora.system.service.impl;

import com.hnx.admin.aurora.annotation.Pager;
import com.hnx.admin.aurora.annotation.RepeatValidator;
import com.hnx.admin.aurora.api.cache.CacheAPI;
import com.hnx.admin.aurora.enums.BusinessType;
import com.hnx.admin.aurora.redis.RedisCache;
import com.hnx.admin.aurora.system.domain.dto.dict.entity.DictDataDTO;
import com.hnx.admin.aurora.system.domain.dto.dict.entity.DictTypeDTO;
import com.hnx.admin.aurora.system.domain.param.dict.form.DictTypeForm;
import com.hnx.admin.aurora.system.domain.param.dict.query.DictDataQuery;
import com.hnx.admin.aurora.system.domain.param.dict.query.DictQuery;
import com.hnx.admin.aurora.system.mapper.dict.DictDataMapper;
import com.hnx.admin.aurora.system.mapper.dict.DictTypeMapper;
import com.hnx.admin.aurora.system.service.SysDictTypeService;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
创建人： 倔强的头发
创建时间：2025/6/18
功能描述:

*/
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl implements SysDictTypeService, CacheAPI {
    private final RedisCache redisCache;
    private final DictTypeMapper dictTypeMapper;
    private final DictDataMapper dictDataMapper;
    @Override
    public ResultJson getInfo(String dictId) {
        DictTypeDTO dictTypeDTO = dictTypeMapper.selectDictTypeById(dictId);
        return ResultJson.data(dictTypeDTO);
    }

    @Override
    @Pager
    public ResultJson selectDictTypeList(DictQuery dictType) {
        List<DictTypeDTO> list = dictTypeMapper.selectDictTypeList(dictType);
        return ResultJson.rows(list);
    }

    @Override
    @RepeatValidator
    public ResultJson insertDictType(DictTypeForm dict) {
        int res = dictTypeMapper.insertDictType(dict);
        if (res > 0) {
            if (redisCache.hasKey(getDictKey(dict.getDictType()))) {
                redisCache.deleteObject(getDictKey(dict.getDictType()));
            }
        }
        return ResultJson.condition(res);
    }

    @Override
    public ResultJson selectDictTypeAll() {
        List<DictTypeDTO> list = dictTypeMapper.selectDictTypeAll();
        return ResultJson.data(list);
    }

    @Override
    public ResultJson deleteDictTypeByIds(String[] dictIds) {
        int res = dictDataMapper.countDictDataByType(dictIds);
        if (res > 0) {
            throw new RuntimeException(String.format("字典类型已分配,不能删除"));
        }
        int result = dictTypeMapper.deleteDictTypeById(dictIds);
        return ResultJson.success();
    }

    @Override
    @RepeatValidator(type = BusinessType.UPDATE)
    public ResultJson updateDictType(DictTypeForm dict) {
        int res = dictTypeMapper.updateDictType(dict);
        if (res > 0) {
            if (redisCache.hasKey(getDictKey(dict.getDictType()))) {
                redisCache.deleteObject(getDictKey(dict.getDictType()));
            }
        }
        return ResultJson.condition(res);
    }
    /**
     * 清空字典缓存
     */
    public void clearDictCache() {
        Collection<String> keys = redisCache.keys(SYS_DICT_KEY + "*");
        redisCache.deleteObject(keys);
    }

    private void loadingDictCache() {
        DictDataQuery dictData = new DictDataQuery();
        dictData.setStatus("0");
        List<DictDataDTO> list = dictDataMapper.selectDictDataList(dictData);
        Map<String, List<DictDataDTO>> collect = list.stream().collect(Collectors.groupingBy(DictDataDTO::getDictType));
        collect.forEach((key, items) -> {
            redisCache.setCacheObject(SYS_DICT_KEY + key, items.stream().sorted(Comparator.comparing(DictDataDTO::getDictSort)).collect(Collectors.toList()));
        });
    }
    @Override
    public ResultJson resetDictCache() {
        clearDictCache();
        loadingDictCache();
        return ResultJson.success();
    }


}
